package top.kaoshanji.p3ch13a;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import javax.security.auth.Subject;
import javax.security.auth.login.LoginContext;
import javax.security.auth.login.LoginException;

/**
 * 代码清单-p3ch13a-01-415页a：完整的登录过程的示例
 * author kaoshanji
 * ^_^
 * create time 2025/2/7 19:02
 */
public class MyApp {

    private static final Logger logger = LogManager.getLogger(MyApp.class);

    public static void main(String[] args) throws LoginException {
        logger.info("...代码清单-p3ch13a-01-415页a：完整的登录过程的示例...");

        MyApp myApp = new MyApp();
        Subject subject = myApp.login();
        System.out.println(subject);
        myApp.logout();
        System.out.println(subject);
    }

    private LoginContext loginContext;

    public MyApp() throws LoginException {
        TextCallbackHandler callbackHandler = new TextCallbackHandler();
        loginContext = new LoginContext("MyApp", callbackHandler);
    }

    public Subject login() throws LoginException {
        loginContext.login();
        return loginContext.getSubject();
    }

    public void logout() throws LoginException {
        loginContext.logout();
    }


}
